<template>
  <div class="department-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.deptName"
              clearable
              placeholder="请输入名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      :data="list"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
<!--      <el-table-column show-overflow-tooltip type="selection" />-->
      <el-table-column label="部门名称" prop="deptName" show-overflow-tooltip />
      <el-table-column label="排序" prop="orderNum" show-overflow-tooltip />
      <el-table-column
        label="创建时间"
        prop="createTime"
        show-overflow-tooltip
      />
      <el-table-column label="操作" width="85">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button v-if="row.id != 1"
            :disabled="!row.parentId"
            type="text"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import {handleTree} from  "@/utils/treeselectHandle"
  // import { doDelete, getList } from '@/api/departmentManagement'
  import {  getList ,doDelete } from '@/api/departmentManagement'
  import Edit from './components/DepartmentManagementEdit'

  export default {
    name: 'DepartmentManagement',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        queryForm: {
          deptName: undefined
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row){
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { status ,message } = await doDelete(row.id)
            if (status != 200){
              this.$baseMessage(message, 'error', 'vab-hey-message-success')
            }else {
              this.$baseMessage(message, 'success', 'vab-hey-message-success')
            }
            await this.fetchData()
          })
        }
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
           data
        } = await getList(this.queryForm)
        this.list = handleTree(data, "id");
        this.listLoading = false
      },
    },
  }
</script>
